<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import useUserStore from '@/stores/useUserStore.js'
import { vercode, mobileRegistration, postLogin } from '@/apis/apis.js'
import emitter from '@/utils/mitt.js'

emitter.on('openLogin', (payload) => {
  show.value = payload.show
})

const show = ref(false)
const mode = ref(true)
const send = ref(false)
const phone = ref('')
const password = ref('')
const code = ref('')
const bizId = ref('')
const userStore = useUserStore()
const router = useRouter()

const handleClose = () => {
  phone.value = ''
  password.value = ''
  code.value = ''
  mode.value = true
  send.value = false
}

const toCodeLogin = () => {
  mode.value = false
  send.value = false
  phone.value = ''
  password.value = ''
}

const toPasswordLogin = () => {
  mode.value = true
  send.value = false
  phone.value = ''
  code.value = ''
}

const handleFinish = () => {
  send.value = false
}

const getVercode = async () => {
  if (!/^1[3-9]\d{9}$/.test(phone.value)) {
    showToast({ message: '请输入正确的手机号', duration: 500 })
    return
  }
  send.value = true
  const res = await vercode({ phoneNumbers: phone.value })
  bizId.value = res.data.bizId
}

const login = async () => {
  if (!/^1[3-9]\d{9}$/.test(phone.value)) {
    showToast({ message: '请输入正确的手机号', duration: 500 })
    return
  }
  if (mode.value) {
    if (!/^[a-zA-Z\d]{6,12}$/.test(password.value)) {
      showToast({ message: '请输入正确的密码', duration: 500 })
      return
    }
    const res = await postLogin({ mobile: phone.value, password: password.value })
    userStore.updateUser(res.data)
    show.value = false
    location.reload(true)
  }
  else {
    if (!/^\d{4}$/.test(code.value)) {
      showToast({ message: '请输入正确的验证码', duration: 500 })
      return
    }
    try {
      const res = await mobileRegistration({ mobile: phone.value, code: code.value, bizId: bizId.value })
      userStore.updateUser(res.data)
      show.value = false
      location.reload(true)
    }
    catch (err) {
      showToast({ message: err.response.data.msg, duration: 500 })
    }
  }
}

const toPasswordSettingsView = () => {
  show.value = false
  router.push('/password-settings')
}
</script>

<template>
  <van-popup v-model:show="show" position="bottom" closeable close-icon-position="top-left" @close="handleClose">
    <div class="title">登录后更精彩</div>
    <div class="subtitle">全世界的旅行故事都在期待与你的相遇</div>
    <van-field v-model="phone" type="tel" label="手机号" label-width="1.5rem" placeholder="请输入手机号" clearable />
    <van-field v-model="password" type="password" label="密码" label-width="1.5rem" placeholder="请输入密码" clearable
      v-if="mode" />
    <van-field v-model="code" type="number" center clearable label="验证码" label-width="1.5rem" placeholder="请输入验证码"
      v-else>
      <template #button>
        <van-button size="small" type="success" :disabled="send" @click="getVercode">
          <template #default>
            <span v-if="!send">发送验证码</span>
            <van-count-down v-else :time="60 * 1000" format="ss秒后重新获取" @finish="handleFinish" />
          </template>
        </van-button>
      </template>
    </van-field>
    <van-button class="login" type="success" size="large" @click="login">登录</van-button>
    <div class="footer" v-if="mode">
      <span class="code" @click="toCodeLogin">验证码登录</span>
      <span @click="toPasswordSettingsView">忘记密码</span>
    </div>
    <div class="footer" v-else>
      <span @click="toPasswordLogin">账号登录</span>
    </div>
  </van-popup>
</template>

<style scoped lang="scss">
.van-popup {
  padding: 80px 20px;
  height: 100vh;
  background-image: url(@/assets/login-bg.jpg);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  --van-count-down-text-color: white;
  --van-count-down-font-size: 12px;

  .title {
    margin-bottom: 20px;
    font-weight: bold;
    font-size: 26px;
  }

  .subtitle {
    margin-bottom: 80px;
    font-size: 18px;
  }

  .login {
    margin-top: 30px;
  }

  .footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-top: 20px;
    color: white;
    font-size: 18px;

    .code {
      margin-right: 8px;
    }
  }
}
</style>
